package org.hot100_3;

public class leetcode283 {

    public void moveZeroes2(int[] nums) {
        int n = nums.length;

        int slow = 0;

        //使用双指针，将所有非0的值往前移动
        for (int fast = 0; fast < n; fast++) {
            if (nums[fast] != 0) {
                nums[slow] = nums[fast];
                slow++;
            }
        }

        //最终slow指向的下标为当前数组非0的个数
        for (int i = slow; i < n; i++) {
            nums[i] = 0;
        }

    }

    public void moveZeroes(int[] nums) {
        int n = nums.length;

        int slow = 0;
        int fast = 0;

        while (fast != n) {
            if (nums[fast] != 0) {
                swap(nums, fast, slow);
                slow++;
            }
            fast++;
        }
    }

    private void swap(int[] nums, int index1, int index2) {
        int temp = nums[index1];
        nums[index1] = nums[index2];
        nums[index2] = temp;
    }
}
